<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="root">
        <header class="nav">
            <button class="sidebar_toggle">
                <svg viewBox="0 0 48 48">
                    <line x1="44" y1="12" x2="4" y2="12"></line>
                    <line x1="44" y1="26" x2="4" y2="26"></line>
                    <line x1="44" y1="40" x2="4" y2="40"></line>
                </svg>
            </button>
            <a class="nav-item active" href="#">Home</a>
            <a class="nav-item" href="#">Team</a>
            <a class="nav-item" href="#">Contact</a>
            <a class="nav-item" href="#">More1111111111111111111111111111111111111111111111111111111111111111</a>
            <div class="indicator"></div>
        </header>
        <main class="content">
            <p>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>
                qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>
                qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>qdeqeq<br>
            </p>
        </main>
    </div>
</body>
<style>
    * {
        padding: 0;
        margin: 0;
    }

    /* html高度要设置为100%或者100vh才能使content占满剩余高度 */
    html,
    body {
        height: 100vh;
    }

    .root {
        height: 100%;
        display: flex;
        flex-direction: column;
    }

    .content {
        background-color: white;
        flex: 1 1 0;
        overflow: auto;
        min-height: 0;
        padding: 6px;
        box-sizing: border-box;
    }

    .nav {
        background-color: #f7f7f7;
        border-bottom: 2px solid #dedede;
        display: flex;
        align-items: center;
        position: relative;
        box-sizing: border-box;
    }

    .nav .indicator {
        position: absolute;
        background-color: steelblue;
        left: 0;
        width: 0;
        top: 100%;
        height: 2px;
        transition: all .2s;
    }

    .anim-none {
        transition: all 0s !important;
    }

    .nav-item {
        display: block;
        text-align: center;
        height: 30px;
        padding: 6px 12px;
        cursor:pointer;
        color: black;
        text-decoration: none;
    }

    .nav-item:hover, .active {
        color: steelblue;
        background-color: antiquewhite;
    }

    .sidebar_toggle {
        flex-shrink: 0;
        background: transparent;
        border: transparent;
        cursor: pointer;
        height: 1.2rem;
        width: 1.2rem;
        margin: 0 12px;
    }

    .sidebar_toggle svg {
        fill: none;
        stroke: #374151;
        stroke-linecap: round;
        stroke-linejoin: round;
        stroke-width: 4px;
    }
</style>
<script>
    const nav = document.querySelector(".nav");
    const items = Array.from(nav.querySelectorAll(".nav-item"));
    const indicator = nav.querySelector(".indicator");
    // begin 解决指示条首次加载时出现动画的问题
    document.body.onload = function() {
        indicator.classList.toggle('anim-none')
    }
    indicator.classList.toggle('anim-none');
    // end
    // offsetLeft为元素相对于最近的relative元素的偏移
    indicator.style.left = items[0].offsetLeft + 'px';
    indicator.style.width = items[0].offsetWidth + 'px';
    function clickHandle(e) {
        indicator.style.left = e.target.offsetLeft + 'px';
        indicator.style.width = e.target.offsetWidth + 'px';
        items.forEach((i) => i.classList.remove("active"));
        e.target.classList.add("active");
    }
    for(let i=0; i<items.length; i++) {
        items[i].addEventListener('click', clickHandle);
    }
</script>
</html>